/*
 * GoMokuView.java
 */

package gomoku;

import gomoku.Controller.GoMokuGameLogic;
import org.jdesktop.application.Action;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import java.awt.Point;
import gomoku.Controller.GoMokuGameType;


/**
 * The application's main frame.
 */
public class GoMokuAppView extends FrameView implements GoMokuActionListener {


    public GoMokuAppView(SingleFrameApplication app) {
        super(app);
        initComponents();
                
        this.getFrame().setTitle("GoMoku Game");
        this.getFrame().setResizable(false);

        gamePanel.registerMoveHandler(gamePanel);
        gamePanel.registerMoveHandler(this);
        
        gamePanel.newGame(GoMokuGameType.UserVSUser);
    }

    public void makeMove(Point location) {
        saveMenuItem.setEnabled(true);
    }
    
    public void newGame(GoMokuGameType type) {
        saveMenuItem.setEnabled(false);
    }
    
    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = GoMokuApp.getApplication().getMainFrame();
            aboutBox = new GoMokuAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        GoMokuApp.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        newMenuItem = new javax.swing.JMenuItem();
        openMenuItem = new javax.swing.JMenuItem();
        saveMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        gamePanel = new gomoku.GamePanel();

        menuBar.setName("menuBar"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(gomoku.GoMokuApp.class).getContext().getResourceMap(GoMokuAppView.class);
        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(gomoku.GoMokuApp.class).getContext().getActionMap(GoMokuAppView.class, this);
        newMenuItem.setAction(actionMap.get("startNewGame")); // NOI18N
        newMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
        newMenuItem.setText(resourceMap.getString("newMenuItem.text")); // NOI18N
        newMenuItem.setName("newMenuItem"); // NOI18N
        fileMenu.add(newMenuItem);

        openMenuItem.setAction(actionMap.get("openGame")); // NOI18N
        openMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
        openMenuItem.setText(resourceMap.getString("openMenuItem.text")); // NOI18N
        openMenuItem.setName("openMenuItem"); // NOI18N
        fileMenu.add(openMenuItem);

        saveMenuItem.setAction(actionMap.get("saveGame")); // NOI18N
        saveMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        saveMenuItem.setText(resourceMap.getString("saveMenuItem.text")); // NOI18N
        saveMenuItem.setEnabled(false);
        saveMenuItem.setName("saveMenuItem"); // NOI18N
        fileMenu.add(saveMenuItem);

        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        gamePanel.setName("gamePanel"); // NOI18N

        setComponent(gamePanel);
        setMenuBar(menuBar);
    }// </editor-fold>//GEN-END:initComponents


    @Action
    public void startNewGame() {
        gamePanel.newGame(GoMokuGameType.UserVSUser);
    }

    @Action
    public void openGame() {
    
        JFileChooser menu = new JFileChooser();
        menu.setDialogType(JFileChooser.OPEN_DIALOG);
        menu.setDialogTitle("Load Game");

        int res = menu.showOpenDialog(null);
            
        if(res == JFileChooser.APPROVE_OPTION)
        {
                if(menu.getSelectedFile().exists())
                {
                    if (gamePanel.LoadGame(menu.getSelectedFile()) == true) {
                        // disable the save option
                        saveMenuItem.setEnabled(false);
                    }                    
                } else {
                    JOptionPane.showMessageDialog(null,"Error opening game. File not found.");
                }
        }
    }

    @Action
    public void saveGame() {
        JFileChooser menu = new JFileChooser();
        menu.setDialogType(JFileChooser.SAVE_DIALOG);
        menu.setDialogTitle("Save Game");

        int res = menu.showOpenDialog(null);
            
        if(res == JFileChooser.APPROVE_OPTION)
        {
                if(menu.getSelectedFile().exists())
                {
                    // file exists but we won't overwrite
                    JOptionPane.showMessageDialog(null,"Error saving game. This file already exists.");
                    //menu.getSelectedFile();
                } else {
                    gamePanel.SaveGame(menu.getSelectedFile());
                }
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private gomoku.GamePanel gamePanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem newMenuItem;
    private javax.swing.JMenuItem openMenuItem;
    private javax.swing.JMenuItem saveMenuItem;
    // End of variables declaration//GEN-END:variables


    private JDialog aboutBox;
}
